<?php
include_once("game_repository.php");
include_once("doubles_repository.php");
$repGame = new GameRepository;
$repDoubles = new DoublesRepository; 

$matchID=array();
for ($i=1;$i<10;$i++){
	$matchID[$i] =$_POST['matchID_'.$i];
	$player1_score1 =$_POST['g1player1_'.$matchID[$i]];
	$player1_score2 =$_POST['g2player1_'.$matchID[$i]];
	$player1_score3 =$_POST['g3player1_'.$matchID[$i]];
	$player2_score1 =$_POST['g1player2_'.$matchID[$i]];
	$player2_score2 =$_POST['g2player2_'.$matchID[$i]];
	$player2_score3 =$_POST['g3player2_'.$matchID[$i]]; 
	
	if (($player1_score1>=0)&&($player2_score1>=0)){

			if ($repGame->readGame($matchID[$i],1)!=""){
				$repGame->updateGame($matchID[$i],1,$player1_score1,$player2_score1);	
			}
			else{
				$repGame->createGame($matchID[$i],1,$player1_score1,$player2_score1);
			}
		}
		if (($player1_score2>=0)&&($player2_score2>=0)){
			if ($repGame->readGame($matchID[$i],2)!=""){
				$repGame->updateGame($matchID[$i],2,$player1_score2,$player2_score2);
			}
			else{
				$repGame->createGame($matchID[$i],2,$player1_score2,$player2_score2);
			}
		}
		if (($player1_score3>=0)&&($player2_score3>=0)){
			if ($repGame->readGame($matchID[$i],3)!=""){
				$repGame->updateGame($matchID[$i],3,$player1_score3,$player2_score3);
			}
			else{
				$repGame->createGame($matchID[$i],3,$player1_score3,$player2_score3);
			}
		}
}

		$team1Points=array(
		$_POST['team1Points0'],
		$_POST['team1Points1'],
		$_POST['team1Points2'],
		$_POST['team1Points3'],
		$_POST['team1Points4'],
		$_POST['team1Points5']);
		$team2Points=array(
		$_POST['team2Points0'],
		$_POST['team2Points1'],
		$_POST['team2Points2'],
		$_POST['team2Points3'],
		$_POST['team2Points4'],
		$_POST['team2Points5']);
		
		$roundSchedule=$_POST['scaduleId'];
		
		for ($j=0; $j<6; $j++){	
			if (($team1Points[$j]>=0)&&($team2Points[$j]>=0)){

				if (count($repDoubles->getDoublesByScheduleIDAndGameNo($roundSchedule,$j+1))!=0){
					$repDoubles->updateDoubles($roundSchedule,$j+1,$team1Points[$j],$team2Points[$j]);

				}
				else{
					$repDoubles->createDoubles($roundSchedule,$j+1,$team1Points[$j],$team2Points[$j]);

				}
			}	
		}			
header('LOCATION:'.$_SERVER["HTTP_REFERER"]);


		
?>